﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;

namespace OAuth
{
    public static class OAuthHttpRequestFactory
    {
        public static OAuthHttpRequest Create(string url, OAuthHttpMethod method)
        {
            ParamValidator.ValidateNullOrEmpty(url, "url is empty", "url");

            Uri uri = new Uri(url);

            if (uri.Scheme != Uri.UriSchemeHttp && uri.Scheme != Uri.UriSchemeHttps)
            {
                throw new ArgumentException("Invalid protocol", "url");
            }

            var result = new OAuthHttpRequest() { RequestUrl = uri };

            switch (method)
            {
                case OAuthHttpMethod.Get:
                    result.HttpMethod = WebRequestMethods.Http.Get;
                    break;
                case OAuthHttpMethod.Post:
                    result.HttpMethod = WebRequestMethods.Http.Post;
                    break;
                default:
                    throw new ArgumentException("Unsupported HTTP method", "method");
            }

            var query = HttpUtility.ParseQueryString(uri.Query);

            foreach(string key in query)
            {
                result.RequestParameter[key] = query[key];
            }

            return result;
        }
    }
}
